/**
 * 
 */
package com.feib.soeasy.dao.jdbc;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.Bp004Dao;

/**
 * @author Jimmy Liu
 *
 */
@Repository("bp004Dao")
public class Bp004JdbcDao extends JdbcDaoSupport implements Bp004Dao {

	@Resource(name = "dataSource")
	public void setMyDataSource(DataSource dataSource) {
		super.setDataSource(dataSource); 
	}   


	/* (non-Javadoc)
	 * @see com.feib.soeasy.dao.Bp004Dao#isHoliday(java.util.Date)
	 */
	@Override
	public boolean isHoliday(Date now) {

		/* 20120521 ACH銷帳使用processDate 轉民國年    系統8碼民國年日期(01000426) */
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

		String processSysDateROC = String.format( "%1$08d", Integer.parseInt(dateFormat.format(now)) - 19110000); 

		StringBuffer sql = new StringBuffer();
		List<Object> args = new ArrayList<Object>();

		/* 使用processDate 轉民國年取得營業日 */
		sql.append("Select * From AR.dbo.BP004 where BP004_TBSDY = ?");
		args.add(processSysDateROC);

		List<Map<String, Object>> rds = this.getJdbcTemplate().queryForList(sql.toString(), args.toArray());

		/* 判斷是否為假日 */
		if (rds.size()!=0)
		{
			String holiday = null;
			for(Map<String, Object> rd : rds){
				holiday = ((String)rd.get("BP004_HOLIDY"));
				if ("000001".equals(holiday)){ 
					return true;
				}
			}
		}

		return false;
	}

}
